package com.share.common.utils;


import org.apache.commons.lang3.StringUtils;

public class BankCardUtil {

    public static final String bankCardArr[][] = {{"招商银行", "16", "zhaoshangyinhang", "31988,356889,439225,439226,439227,518710,518718"}, {"工商银行", "19", "gongshangyinhang", "622203,621476,622202,621226,622200,955880,955881,955882"}, {"农业银行", "19", "nongyeyinhang", "62284,621336,621282"}, {"建设银行", "16", "jiansheyinhang", "621700"}};

    //返回16位 或者 19位银行卡号码
    public static String gen() {
        int length = bankCardArr.length;
        Integer i = UUIDUtil.genRandomInt(length);
        String name = bankCardArr[i][0];
        Integer len = new Integer(bankCardArr[i][1]);

        String[] tmpArr = bankCardArr[i][3].split(",");
        Integer tmpI = UUIDUtil.genRandomInt(tmpArr.length);
        String code = tmpArr[tmpI];

        String bankCard = code + UUIDUtil.genRandomNumber(len - code.length());
        return bankCard;
    }

    //通过银行卡号  返回 银行卡编码
    public static String getBankCode(String bankCardNo) {
        String re = null;
        if (bankCardNo != null && bankCardNo.length() >= 6) {
            //前6位识别
            String pre = bankCardNo.substring(0, 6);
            int length = bankCardArr.length;
            for (int i = 0; i < length; i++) {
                String bankCardNoPre = bankCardArr[i][3];
                if (bankCardNoPre.contains(pre)) {
                    re = bankCardArr[i][2];
                    break;
                }
            }

            //前5位识别
            if (StringUtils.isEmpty(re)) {
                pre = bankCardNo.substring(0, 5);
                for (int i = 0; i < length; i++) {
                    String bankCardNoPre = bankCardArr[i][3];
                    if (bankCardNoPre.contains(pre)) {
                        re = bankCardArr[i][2];
                        break;
                    }
                }
            }
        }
        return re;
    }

    //返回银行的名字
    public static String getBankName(String bankCardNo) {
        String re = null;
        if (bankCardNo != null && bankCardNo.length() >= 6) {
            //前6位识别
            String pre = bankCardNo.substring(0, 6);
            int length = bankCardArr.length;
            for (int i = 0; i < length; i++) {
                String bankCardNoPre = bankCardArr[i][3];
                if (bankCardNoPre.contains(pre)) {
                    re = bankCardArr[i][0];
                    break;
                }
            }

            //前5位识别
            if (StringUtils.isEmpty(re)) {
                pre = bankCardNo.substring(0, 5);
                for (int i = 0; i < length; i++) {
                    String bankCardNoPre = bankCardArr[i][3];
                    if (bankCardNoPre.contains(pre)) {
                        re = bankCardArr[i][0];
                        break;
                    }
                }
            }
        }
        return re;
    }

    public static void main(String[] args) {
        System.out.println(getBankCode("4392279214067786"));
    }
}
